Imports KanBan.Pgd.Infrastructure.Exceptions
Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Infrastructure.Repositories

Public Class EditUser

    Public Event UserAdded(ByVal user As User)
    Public Event UserUpdated(ByVal user As User)

#Region "IEditUserView"

    Private _id As Guid
    Public Property Id() As System.Guid
        Get
            Return _id
        End Get
        Set(ByVal value As System.Guid)
            _id = value
        End Set
    End Property

    Public Sub New()

        Try

            ' This call is required by the Windows Form Designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            Me.FormMode = FormMode.Add
            Me.FormTitle = "Adding New User"

        Catch ex As Exception

            FormHelper.ShowError(ExceptionHandler.HandleException(ex))

        End Try

    End Sub

    Public Sub New(ByVal userId As Guid)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.FormMode = FormMode.Edit
        Me.LoadSelectableProperties()
        Me.ContextualizeToEditMode()

        Dim userRepository As New UserRepository()
        Dim user As User = userRepository.GetById(userId)

        Me.FormTitle = String.Format("Editing User : {0}", user.Name)

        Me.Id = user.Id
        Me.NameOfUser = user.Name
        Me.UserName = user.UserName
        Me.ProfileId = user.Profile.Id

    End Sub

    Private Sub LoadSelectableProperties()

        Dim repository As New ProfileRepository()

        Dim profiles As IEnumerable(Of Profile) = repository.GetAll()

        Me.AddProfiles(profiles)

    End Sub

    Public Property NameOfUser() As String
        Get
            Return txtName.Text
        End Get
        Set(ByVal value As String)
            txtName.Text = value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return txtUserName.Text
        End Get
        Set(ByVal value As String)
            txtUserName.Text = value
        End Set
    End Property

    Public Property Password() As String
        Get
            Return txtPassword.Text
        End Get
        Set(ByVal value As String)
            txtPassword.Text = value
        End Set
    End Property

    Public Property ProfileId() As Guid
        Get
            Return CType(cmbProfiles.SelectedValue, Guid)
        End Get
        Set(ByVal value As Guid)
            cmbProfiles.SelectedValue = value
        End Set
    End Property

    Private _formMode As FormMode
    Public Property FormMode() As FormMode
        Get
            Return _formMode
        End Get
        Set(ByVal value As FormMode)
            _formMode = value
        End Set
    End Property

    Public Property FormTitle() As String
        Get
            Return txtFormTitle.Text
        End Get
        Set(ByVal value As String)
            txtFormTitle.Text = value
        End Set
    End Property

    Public Sub AddProfiles(ByVal profiles As IEnumerable(Of Profile))

        cmbProfiles.DataSource = profiles
        cmbProfiles.ValueMember = "Id"
        cmbProfiles.DisplayMember = "Name"

    End Sub

    Public Sub ContextualizeToEditMode()

        Me.lblPassword.Visible = False
        Me.txtPassword.Visible = False
        Me.lblConfirmPassword.Visible = False
        Me.txtConfirmPassword.Visible = False

    End Sub

    Public Sub RaiseUserAddedEvent(ByVal user As User)
        RaiseEvent UserAdded(user)
    End Sub

    Public Sub RaiseUserUpdatedEvent(ByVal user As User)
        RaiseEvent UserUpdated(user)
    End Sub

#End Region

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If Me.FormMode = Presentation.FormMode.Add Then
            If Not IsAddingValid() Then
                Exit Sub
            End If
        End If

        If Me.FormMode = Presentation.FormMode.Edit Then
            If Not IsEditingValid() Then
                Exit Sub
            End If
        End If

        Me.SaveOrUpdateUser()
        Me.Close()

    End Sub

    Public Sub SaveOrUpdateUser()

        Dim profileRepository As New ProfileRepository()
        Dim profile As Profile = profileRepository.GetById(Me.ProfileId)

        Dim userRepository As New UserRepository()
        Dim user As User = userRepository.GetById(Me.Id)

        If user Is Nothing Then

            user = New User(Me.NameOfUser, _
                            Me.UserName, _
                            Me.Password, _
                            profile)

            Me.RaiseUserAddedEvent(user)

        Else

            user.Name = Me.NameOfUser
            user.UserName = Me.UserName
            user.Profile = profile

            Me.RaiseUserUpdatedEvent(user)

        End If

        userRepository.SaveOrUpdate(user)

    End Sub

#Region "Validation"

    Private Function IsAddingValid() As Boolean

        Dim isValid As Boolean = IsNameValid() And _
                                 IsUserNameValid() And _
                                 IsPasswordValid()

        Return isValid

    End Function

    Private Function IsEditingValid() As Boolean

        Dim isValid As Boolean = IsNameValid() And _
                                 IsUserNameValid()

        Return isValid

    End Function

    Private Function IsNameValid() As Boolean

        If txtName.Text = String.Empty Then
            errorProvider.SetError(txtName, "You must fill in a value for 'Name'")
            Return False
        Else
            errorProvider.SetError(txtName, String.Empty)
            Return True
        End If
    End Function

    Private Function IsUserNameValid() As Boolean

        If txtUserName.Text = String.Empty Then
            errorProvider.SetError(txtUserName, "You must fill in a value for 'User Name'")
            Return False
        Else
            errorProvider.SetError(txtUserName, String.Empty)
            Return True
        End If
    End Function

    Private Function IsPasswordValid() As Boolean

        If txtPassword.Text = String.Empty Then
            errorProvider.SetError(txtPassword, "You must fill in a value for 'Password'")
            Return False
        ElseIf txtPassword.Text <> txtConfirmPassword.Text Then
            errorProvider.SetError(txtPassword, "'Password' and 'Confirm Password' fields must match")
            errorProvider.SetError(txtConfirmPassword, "'Password' and 'Confirm Password' fields must match")
            Return False
        Else
            errorProvider.SetError(txtPassword, String.Empty)
            Return True
        End If
    End Function

#End Region

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

End Class